const db = require('better-sqlite3')('./data/delivery-management.db');

console.log('查找圆筒形储罐相关信息...');

// 查找圆筒形储罐的设备信息
const devices = db.prepare("SELECT * FROM devices WHERE name LIKE '%圆筒%' OR classification_name LIKE '%圆筒%'").all();
console.log('设备信息:');
devices.forEach(d => {
  console.log('设备ID:', d.device_id);
  console.log('设备名称:', d.name);
  console.log('分类名称:', d.classification_name);
  console.log('分类码:', d.classification_code);
  console.log('---');
});

// 查找设备类型映射
const mappings = db.prepare("SELECT * FROM device_type_mappings WHERE classification_name LIKE '%圆筒%'").all();
console.log('\n设备类型映射:');
mappings.forEach(m => {
  console.log('分类名称:', m.classification_name);
  console.log('分类码:', m.classification_code);
  console.log('基础参数表:', m.base_params_table);
  console.log('---');
});

// 如果找到了参数表，检查其中的单位信息
if (mappings.length > 0) {
  const mapping = mappings[0];
  console.log(`\n检查参数表 ${mapping.base_params_table} 的字段信息:`);
  
  try {
    const columns = db.prepare(`PRAGMA table_info(${mapping.base_params_table})`).all();
    console.log('表字段:');
    columns.forEach(col => {
      console.log(`  ${col.name}: ${col.type}`);
    });
    
    // 检查表中的数据样本
    const sampleData = db.prepare(`SELECT * FROM ${mapping.base_params_table} LIMIT 3`).all();
    console.log('\n数据样本:');
    sampleData.forEach((row, i) => {
      console.log(`行 ${i+1}:`, JSON.stringify(row, null, 2));
    });
  } catch(e) {
    console.log('查询参数表失败:', e.message);
  }
}

// 检查field_metadata表中是否有相关的字段元数据
console.log('\n检查字段元数据...');
try {
  const fieldMetadata = db.prepare("SELECT * FROM field_metadata WHERE measurement_unit LIKE '%㎡%' OR measurement_unit LIKE '%?%'").all();
  console.log('包含面积单位的字段:');
  fieldMetadata.forEach(field => {
    console.log('字段名:', field.field_name);
    console.log('计量单位:', field.measurement_unit);
    console.log('表名:', field.table_name);
    console.log('---');
  });
} catch(e) {
  console.log('查询字段元数据失败:', e.message);
}

db.close();